/**
 * @Title: 预约指令下发类
 * @author pengyu
 * @date 2015年12月15日 上午10:21:55
 * @version V1.0
 */
package tt.dz.mvc.controller.middleware;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForHardware;
import tt.dz.entity.charger.OmsCharger;
import tt.dz.entity.charger.OmsChargerBespeakBillRecord;
import tt.dz.entity.exception.BizException;
import tt.dz.mvc.controller.base.BaseController;
import tt.dz.service.middleware.OmsBespeakService;
import tt.dz.service.sys.BillService;
import tt.dz.service.sys.ChargerService;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

@Controller
public class BespeakController extends BaseController
{
	private Log log = LogFactory.getLog(BespeakController.class);

	@Autowired
	public BillService billService;

	@Autowired
	public ChargerService chargerService;

	@Autowired
	OmsBespeakService omsBespeakService;
	//  3.1.4. 开始预约    AS 调用OMS，OMS请求中间件  api_v3.php中的bespeakDianzhuang方法
	//AS 请求 OMS ，OMS 下发充电 socket，只下发，短连接  
	//	2.1版本需要发送预约费用 ,单独 一个接口   是哪边生成预约流水号。   什么时候AS传达费用信息给oms
	//操作流水号	bespeak_jrnl	操作流水号:如果2.0中AS生成发给OMS，就存在OMS中；如果不存，则OMS自动根据时间\电桩地址和其他规则生成；此字段用来关联充电费用记录。0000000000为非真实数据。		varchar(50)	50		FALSE	TRUE	FALSE
	@RequestMapping("/bespeak") 
	@ResponseBody
	public JsonCommon<String> bespeak(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,String> map = StringUtil.getParameterByOms(request);
//		String portNo = request.getParameter("portNo");//AS传值
//		String chargerId = request.getParameter("chargerId");//AS传值
//		String userCard = request.getParameter("userCard");//AS传值
//		String bespeakMinute = request.getParameter("bespeakMinute");//AS传值  预约时长
//		return omsBespeakService.bespeak(chargerId,portNo,userCard,bespeakMinute);
		return omsBespeakService.bespeak(map);
//		return "sendSocket success";
	}	
	
	//AS 请求 OMS ，OMS 下发预约返回  后:中间件回复http，OMS同步返回报文头     api_v3.php中的  controlBespeakReturn($thisDate,"1");
	@RequestMapping("/bespeakReturn") 
	@ResponseBody
	public JsonCommon<String> bespeakReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,String> map = StringUtil.getParameterByMiddle(request);
		omsBespeakService.bespeakReturn(map);
		return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}	

	//	public JsonCommon<String> cancelBespeak(String bespeakStartSerialNumber) throws Exception {
	//3.1.5. 结束预约     AS 请求 OMS ，OMS 下发充电 socket，只下发，短连接  startChargeReturn   api_v3.php中的  cancelBespeakDianzhuang 
	@RequestMapping("/cancelBespeak") 
	@ResponseBody
	public JsonCommon<String> cancelBespeak(HttpServletRequest request, HttpServletResponse response) throws Exception {		
		String bespeakStartSerialNumber = request.getParameter("bespeakStartSerialNumber");

		OmsChargerBespeakBillRecord queryBespeakBill = new OmsChargerBespeakBillRecord();
		queryBespeakBill.setOms_msg_bespeak_jrnl(bespeakStartSerialNumber);
		queryBespeakBill = this.billService.findBespeakBillOneExample(queryBespeakBill, null, null);
		if(queryBespeakBill == null)
			throw new BizException("Reservation record is not found");

		OmsCharger queryCharger = this.chargerService.findChargerDetailByChargerId(queryBespeakBill.getCharger_id());
		if(queryCharger == null)
			throw new BizException("Charger is not found");

		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("protocol_type",queryCharger.getProtocol_type());
		paramMap.put("protocol_version", queryCharger.getProtocol_version());
		paramMap.put("chargerId", queryBespeakBill.getCharger_id());
		paramMap.put("userCard", queryBespeakBill.getUser_pay_card());
		paramMap.put("portNo", String.valueOf(queryBespeakBill.getPort_no()));
		paramMap.put("bespeakStartSerialNumber", queryBespeakBill.getOms_msg_bespeak_jrnl());

		return omsBespeakService.cancelBespeak(paramMap);
	}		
	//AS 请求 OMS ，OMS 下发取消预约 后:中间件回复http，同步返回报文头  controlBespeakReturn
	@RequestMapping("/cancelBespeakReturn") 
	@ResponseBody
	public JsonCommon<String> cancelBespeakReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,String> map = StringUtil.getParameterByMiddle(request);
		omsBespeakService.cancelBespeakReturn(map);
		return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}			
	
	
}
